/*******************************************************************************
 * Package: com.hngtrust.iir.base.controller
 * Type:    DictController
 * Date:    2020-11-30 10:59
 *
 * Copyright (c) 2020 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved.
 *
 * You may not use this file except in compliance with the License.
 *******************************************************************************/
package com.hngtrust.iir.base.controller;

import com.hngtrust.framework.bind.ResponseInfo;
import com.hngtrust.framework.id.service.MyIdGenerator;
import com.hngtrust.framework.security.util.SecurityUtils;
import com.hngtrust.iir.base.controller.vo.AreaVO;
import com.hngtrust.iir.base.controller.vo.DictVO;
import com.hngtrust.iir.base.controller.vo.FileReqVO;
import com.hngtrust.iir.base.controller.vo.IndustryVO;
import com.hngtrust.iir.base.dto.ReportTreeDTO;
import com.hngtrust.iir.base.dto.TreeDate;
import com.hngtrust.iir.base.entity.Area;
import com.hngtrust.iir.base.entity.Dict;
import com.hngtrust.iir.base.entity.EconomicCircle;
import com.hngtrust.iir.base.entity.Industry;
import com.hngtrust.iir.base.entity.IndustryFinance;
import com.hngtrust.iir.base.entity.IndustryGb;
import com.hngtrust.iir.base.entity.IndustrySw;
import com.hngtrust.iir.base.entity.NoticeType;
import com.hngtrust.iir.base.entity.ReportType;
import com.hngtrust.iir.base.service.AreaService;
import com.hngtrust.iir.base.service.BaseService;
import com.hngtrust.iir.base.service.DictService;
import com.hngtrust.iir.base.service.EconomicCircleService;
import com.hngtrust.iir.base.service.IndustryFinanceService;
import com.hngtrust.iir.base.service.IndustryGbService;
import com.hngtrust.iir.base.service.IndustryService;
import com.hngtrust.iir.base.service.IndustrySwService;
import com.hngtrust.iir.base.service.NoticeTypeService;
import com.hngtrust.iir.base.service.ReportTypeService;
import com.hngtrust.iir.base.service.TreeDateService;
import com.hngtrust.iir.base.util.TreeUtils;
import com.hngtrust.iir.company.controller.vo.AiCompanyReqVO;
import com.hngtrust.iir.constant.Constant;
import com.hngtrust.iir.exception.IirErrorType;
import com.hngtrust.iir.exception.IirException;
import com.hngtrust.iir.material.entity.MaterialLog;
import com.hngtrust.iir.material.entity.PrivateMaterial;
import com.hngtrust.iir.material.service.MaterialLogService;
import com.hngtrust.iir.material.service.PrivateMaterialService;
import com.hngtrust.iir.report.entity.PrivateReport;
import com.hngtrust.iir.report.entity.ReportLog;
import com.hngtrust.iir.report.service.PrivateReportService;
import com.hngtrust.iir.report.service.ReportLogService;
import com.hngtrust.iir.risk.entity.RiskType;
import com.hngtrust.iir.risk.service.RiskTypeService;
import com.hngtrust.iir.user.entity.User;
import com.hngtrust.iir.util.DateUtils;
import com.hngtrust.iir.util.FileUtils;
import com.hngtrust.iir.util.ListUtil;
import com.tiger.web.annotation.DownloadMapping;
import com.tiger.web.service.FileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.security.PermitAll;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.apache.pdfbox.pdmodel.PDDocument;

/**
 * 基础数据操作
 * @author Chentianyong
 * @date 2021-03-16 10:59
 */
@Slf4j
@Api(tags = "基础数据操作")
@RestController
@RequestMapping("/base")
public class BaseController {
    
    @Autowired
    private DictService dictService;
    
    @Autowired
    private AreaService areaService;
    
    @Autowired
    private IndustryService industryService;
    
    @Autowired
    private IndustrySwService industrySwService;
    
    @Autowired
    private IndustryGbService industryGbService;
    
    @Autowired
    private IndustryFinanceService industryFinanceService;
    
    @Autowired
    private EconomicCircleService economicCircleService;
    
    @Autowired
    private NoticeTypeService noticeTypeService;
    
    @Autowired
    private ReportTypeService reportTypeService;
    
    @Autowired
    private RiskTypeService riskTypeService;
    
    @Autowired
    private TreeDateService treeDateService;
    
    @Autowired
    private ReportLogService reportLogService;
    
    @Autowired
    private MaterialLogService materialLogService;
    
    @Autowired
    private BaseService baseService;
    
    @Autowired
    private MyIdGenerator myIdGenerator;
    
    @Autowired
    private PrivateReportService privateReportService;
    
    @Autowired
    private PrivateMaterialService privateMaterialService;
    
    @Autowired
    @Qualifier("minIoFileService")
    private FileService fileService;
    
    //minio地址
    @Value("${tiger.minio.out.address}")
    private String minioAddress;
    
    /**
     * minio文件地址
     */
    @Value("${upload.report.minio}")
    private String minioUpload;
    
    // 本地文件服务
    @Autowired
    @Qualifier("localFileService")
    private FileService localFileService;
    
    /**
     * 字典列表
     */
    @ApiOperation(value = "字典列表", notes = "字典列表")
    @PostMapping("/list-dict")
    public ResponseInfo<List<DictVO>> listDict(
            @ApiParam(value = "类型id") @Valid @RequestBody AiCompanyReqVO req) {
        log.info("BaseController.listDict start...");
    
        Map<String, Dict> dictMap = dictService.queryDict(req.getName());
        List<Dict> list = new ArrayList();
        if(null != dictMap){
            list = new ArrayList(dictMap.values());
        }
        
        List<DictVO> resp = new ArrayList<>();
        ListUtil<DictVO> listUtil = BeanUtils.instantiateClass(ListUtil.class);
        listUtil.copyList(list,resp,DictVO.class);
        
        ResponseInfo<List<DictVO>> data = new ResponseInfo<>();
        data.setData(resp);
        return data;
    }
    
    /**
     * 获取省市区
     */
    @ApiOperation(value = "获取省市区", notes = "获取省市区")
    @PostMapping("/list-area")
    public ResponseInfo<List<AreaVO>> listArea(
            @ApiParam(value = "获取省市区") @Valid @RequestBody AreaVO req) {
        log.info("BaseController.listArea start...");
        
        Area area = new Area();
        BeanUtils.copyProperties(req,area);
        List<Area> list = areaService.queryList(area);
    
        List<AreaVO> resp = new ArrayList<>();
        ListUtil<AreaVO> listUtil = BeanUtils.instantiateClass(ListUtil.class);
        listUtil.copyList(list,resp,AreaVO.class);
        
        ResponseInfo<List<AreaVO>> data = new ResponseInfo<>();
        data.setData(resp);
        return data;
    }
    
    /**
     * 根据级数查询行业列表
     */
    @ApiOperation(value = "根据级数查询行业列表", notes = "根据级数查询行业列表")
    @PostMapping("/list-industry-bylevel")
    public ResponseInfo<List<IndustryVO>> listIndustryByLevel(
            @ApiParam(value = "根据级数查询行业列表") @Valid @RequestBody Integer req) {
        log.info("BaseController.listIndustryByLevel start...");
        
        List<Industry> list = industryService.queryIndustryList(req);
        
        List<IndustryVO> resp = new ArrayList<>();
        ListUtil<IndustryVO> listUtil = BeanUtils.instantiateClass(ListUtil.class);
        listUtil.copyList(list,resp,IndustryVO.class);
        
        ResponseInfo<List<IndustryVO>> data = new ResponseInfo<>();
        data.setData(resp);
        return data;
    }
    
    /**
     * 行业列表
     */
    @ApiOperation(value = "行业列表", notes = "行业列表")
    @PostMapping("/list-industry")
    public ResponseInfo<List<IndustryVO>> listIndustry(
            @ApiParam(value = "行业列表") @Valid @RequestBody IndustryVO req) {
        log.info("BaseController.listIndustry start...");
    
        Industry industry = new Industry();
        BeanUtils.copyProperties(req,industry);
        List<Industry> list = industryService.queryList(industry);
        
        List<IndustryVO> resp = new ArrayList<>();
        ListUtil<IndustryVO> listUtil = BeanUtils.instantiateClass(ListUtil.class);
        listUtil.copyList(list,resp,IndustryVO.class);
    
        ResponseInfo<List<IndustryVO>> data = new ResponseInfo<>();
        data.setData(resp);
        return data;
    }
    
    /**
     * 申万行业列表
     */
    @ApiOperation(value = "申万行业列表", notes = "申万行业列表")
    @PostMapping("/list-swindustry")
    public ResponseInfo<List<IndustryVO>> listSwIndustry(
            @ApiParam(value = "行业列表") @Valid @RequestBody IndustryVO req) {
        log.info("BaseController.listSwIndustry start...");
        
        IndustrySw industry = new IndustrySw();
        BeanUtils.copyProperties(req,industry);
        List<IndustrySw> list = industrySwService.queryList(industry);
        
        List<IndustryVO> resp = new ArrayList<>();
        ListUtil<IndustryVO> listUtil = BeanUtils.instantiateClass(ListUtil.class);
        listUtil.copyList(list,resp,IndustryVO.class);
        
        ResponseInfo<List<IndustryVO>> data = new ResponseInfo<>();
        data.setData(resp);
        return data;
    }
    
    /**
     * 申万行业下一级列表
     */
    @ApiOperation(value = "申万行业下一级列表", notes = "申万行业下一级列表,传行业代码和级数")
    @PostMapping("/list-swindustry-sub")
    public ResponseInfo<List<IndustryVO>> listSwIndustrySub(
            @ApiParam(value = "申万行业下一级列表,传行业代码和级数") @Valid @RequestBody IndustryVO req) {
        log.info("BaseController.listSwIndustrySub start...");
        
        IndustrySw industry = new IndustrySw();
        BeanUtils.copyProperties(req,industry);
    
        List<IndustrySw> list = baseService.listSwIndustrySub(industry);
    
        List<IndustryVO> resp = new ArrayList<>();
        ListUtil<IndustryVO> listUtil = BeanUtils.instantiateClass(ListUtil.class);
        listUtil.copyList(list,resp,IndustryVO.class);
        
        ResponseInfo<List<IndustryVO>> data = new ResponseInfo<>();
        data.setData(resp);
        return data;
    }
    
    /**
     * 申万行业树状列表
     */
    @ApiOperation(value = "申万行业树状列表", notes = "申万行业树状列表，根据level参数返回对应层数")
    @PostMapping("/list-swindustry-tree")
    public ResponseInfo<List<ReportTreeDTO>> listSwindustryTree(
            @ApiParam(value = "申万行业树状列表") @Valid @RequestBody IndustrySw req) {
        log.info("BaseController.listSwindustryTree start...");
        
        Integer level = req.getLevel();
        if(null == level || level <= 0 ){
            level = 2;
        }
        req.setLevel(1);
        List<IndustrySw> list = industrySwService.queryList(req);
    
        List<IndustrySw> list2 = null;
        List<IndustrySw> list3 = null;
        List<IndustrySw> list4 = null;
        if(level > 1){
            req.setLevel(2);
            list2 = industrySwService.queryList(req);
        }
        if(level > 2){
            req.setLevel(3);
            list3 = industrySwService.queryList(req);
        }
        if(level > 3){
            req.setLevel(4);
            list4 = industrySwService.queryList(req);
        }
        
        List<ReportTreeDTO> resp = new ArrayList<>();
        
        for(IndustrySw obj : list){
            ReportTreeDTO tree = new ReportTreeDTO();
            BeanUtils.copyProperties(obj,tree);
            tree.setKey(obj.getId());
            tree.setTitle(obj.getName());
            if(level > 1){
                tree.setChildren(new ArrayList<>());
                Integer i2 = 0;
                IndustrySw obj2 = null;
                if(list2.size() > 0){
                    obj2 = list2.get(i2);
                }
                while(null != obj2){
                    if(null != obj2.getCode() && obj2.getCode().substring(0,2).equals(obj.getCode().substring(0,2))){
                        ReportTreeDTO data = new ReportTreeDTO();
                        BeanUtils.copyProperties(obj2,data);
                        data.setKey(obj2.getId());
                        data.setTitle(obj2.getName());
                        if(level > 2 && list3.size() > 0){
                            data.setChildren(new ArrayList<>());
                            Integer i3 = 0;
                            IndustrySw obj3 = null;
                            if(list3.size() > 0){
                                obj3 = list3.get(i3);
                            }
                            while(null != obj3){
                                if(null != obj3.getCode() && obj3.getCode().substring(0,4).equals(obj2.getCode().substring(0,4))){
                                    ReportTreeDTO data3 = new ReportTreeDTO();
                                    BeanUtils.copyProperties(obj3,data3);
                                    data3.setKey(obj3.getId());
                                    data3.setTitle(obj3.getName());
                                    if(level > 3 && list4.size() > 0){
                                        data3.setChildren(new ArrayList<>());
                                        Integer i4 = 0;
                                        IndustrySw obj4 = null;
                                        if(list4.size() > 0){
                                            obj4 = list4.get(i4);
                                        }
                                        while(null != obj4){
                                            if(null != obj4.getCode() && obj4.getCode().substring(0,6).equals(obj3.getCode().substring(0,6))){
                                                ReportTreeDTO data4 = new ReportTreeDTO();
                                                BeanUtils.copyProperties(obj4,data4);
                                                data4.setKey(obj4.getId());
                                                data4.setTitle(obj4.getName());
                                                data3.getChildren().add(data4);
                                                list4.remove(obj4);
                                                if(i4 >= list4.size()) break;
                                                obj4 = list4.get(i4);
                                            }
                                            else{
                                                ++i4;
                                                if(i4 >= list4.size()) break;
                                                obj4 = list4.get(i4);
                                            }
                                        }
                                    }
                
                                    data.getChildren().add(data3);
                                    list3.remove(obj3);
                                    if(i3 >= list3.size()) break;
                                    obj3 = list3.get(i3);
                                }
                                else{
                                    ++i3;
                                    if(i3 >= list3.size()) break;
                                    obj3 = list3.get(i3);
                                }
                            }
                        }
                        
                        //找到了就删除，提高循环效率
                        tree.getChildren().add(data);
                        list2.remove(obj2);
                        if(i2 >= list2.size()) break;
                        obj2 = list2.get(i2);
                    }
                    else{
                        ++i2;
                        if(i2 >= list2.size()) break;
                        obj2 = list2.get(i2);
                    }
                }
            }
            
            resp.add(tree);
        }
        
        ResponseInfo<List<ReportTreeDTO>> data = new ResponseInfo<>();
        data.setData(resp);
        return data;
    }
    
    /**
     * 国资委行业树状列表
     */
    @ApiOperation(value = "国资委行业树状列表", notes = "国资委行业树状列表，根据level参数返回对应层数")
    @PostMapping("/list-gzwindustry-tree")
    public ResponseInfo<List<ReportTreeDTO>> listGzwindustryTree(
            @ApiParam(value = "国资委行业树状列表") @Valid @RequestBody IndustrySw req) {
        log.info("BaseController.listGzwindustryTree start...");
        
        Integer level = req.getLevel();
        if(null == level || level <= 0 ){
            level = 2;
        }
        List<IndustryFinance> list = industryFinanceService.queryByLevel(1);
        
        List<IndustryFinance> list2 = null;
        List<IndustryFinance> list3 = null;
        List<IndustryFinance> list4 = null;
        if(level > 1){
            list2 = industryFinanceService.queryByLevel(2);
        }
        if(level > 2){
            list3 = industryFinanceService.queryByLevel(3);
        }
        if(level > 3){
            list4 = industryFinanceService.queryByLevel(4);
        }
        
        List<ReportTreeDTO> resp = new ArrayList<>();
        
        for(IndustryFinance obj : list){
            ReportTreeDTO tree = new ReportTreeDTO();
            BeanUtils.copyProperties(obj,tree);
            tree.setKey(obj.getId());
            tree.setTitle(obj.getName());
            tree.setLevel(1);
            if(level > 1){
                tree.setChildren(new ArrayList<>());
                Integer i2 = 0;
                IndustryFinance obj2 = null;
                if(list2.size() > 0){
                    obj2 = list2.get(i2);
                }
                while(null != obj2){
                    if(null != obj2.getCode() && obj2.getCode().substring(0,4).equals(obj.getCode().substring(0,4))){
                        ReportTreeDTO data = new ReportTreeDTO();
                        BeanUtils.copyProperties(obj2,data);
                        data.setKey(obj2.getId());
                        data.setTitle(obj2.getName());
                        data.setLevel(2);
                        if(level > 2 && list3.size() > 0){
                            data.setChildren(new ArrayList<>());
                            Integer i3 = 0;
                            IndustryFinance obj3 = null;
                            if(list3.size() > 0){
                                obj3 = list3.get(i3);
                            }
                            while(null != obj3){
                                if(null != obj3.getCode() && obj3.getCode().substring(0,6).equals(obj2.getCode().substring(0,6))){
                                    ReportTreeDTO data3 = new ReportTreeDTO();
                                    BeanUtils.copyProperties(obj3,data3);
                                    data3.setKey(obj3.getId());
                                    data3.setTitle(obj3.getName());
                                    data3.setLevel(3);
                                    if(level > 3 && list4.size() > 0){
                                        data3.setChildren(new ArrayList<>());
                                        Integer i4 = 0;
                                        IndustryFinance obj4 = null;
                                        if(list4.size() > 0){
                                            obj4 = list4.get(i4);
                                        }
                                        while(null != obj4){
                                            if(null != obj4.getCode() && obj4.getCode().substring(0,8).equals(obj3.getCode().substring(0,8))){
                                                ReportTreeDTO data4 = new ReportTreeDTO();
                                                BeanUtils.copyProperties(obj4,data4);
                                                data4.setKey(obj4.getId());
                                                data4.setTitle(obj4.getName());
                                                data4.setLevel(4);
                                                data3.getChildren().add(data4);
                                                list4.remove(obj4);
                                                if(i4 >= list4.size()) break;
                                                obj4 = list4.get(i4);
                                            }
                                            else{
                                                ++i4;
                                                if(i4 >= list4.size()) break;
                                                obj4 = list4.get(i4);
                                            }
                                        }
                                    }
                                    
                                    data.getChildren().add(data3);
                                    list3.remove(obj3);
                                    if(i3 >= list3.size()) break;
                                    obj3 = list3.get(i3);
                                }
                                else{
                                    ++i3;
                                    if(i3 >= list3.size()) break;
                                    obj3 = list3.get(i3);
                                }
                            }
                        }
                        
                        //找到了就删除，提高循环效率
                        tree.getChildren().add(data);
                        list2.remove(obj2);
                        if(i2 >= list2.size()) break;
                        obj2 = list2.get(i2);
                    }
                    else{
                        ++i2;
                        if(i2 >= list2.size()) break;
                        obj2 = list2.get(i2);
                    }
                }
            }
            
            resp.add(tree);
        }
        
        ResponseInfo<List<ReportTreeDTO>> data = new ResponseInfo<>();
        data.setData(resp);
        return data;
    }
    
    
    /**
     * 国标行业树状列表
     */
    @ApiOperation(value = "国标行业树状列表", notes = "国标行业树状列表，根据level参数返回对应层数")
    @PostMapping("/list-gbindustry-tree")
    public ResponseInfo<List<ReportTreeDTO>> listGbindustryTree(
            @ApiParam(value = "国标行业树状列表") @Valid @RequestBody IndustrySw req) {
        log.info("BaseController.listGbindustryTree start...");
    
        IndustryGb gb = new IndustryGb();
        BeanUtils.copyProperties(req,gb);
        Integer level = gb.getLevel();
        if(null == level || level <= 0 ){
            level = 2;
        }
        gb.setLevel(1);
        List<IndustryGb> list = industryGbService.queryList(gb);
        
        List<IndustryGb> list2 = null;
        List<IndustryGb> list3 = null;
        List<IndustryGb> list4 = null;
        if(level > 1){
            gb.setLevel(2);
            list2 = industryGbService.queryList(gb);
        }
        if(level > 2){
            gb.setLevel(3);
            list3 = industryGbService.queryList(gb);
        }
        if(level > 3){
            gb.setLevel(4);
            list4 = industryGbService.queryList(gb);
        }
        
        List<ReportTreeDTO> resp = new ArrayList<>();
        
        for(IndustryGb obj : list){
            ReportTreeDTO tree = new ReportTreeDTO();
            BeanUtils.copyProperties(obj,tree);
            tree.setKey(obj.getId());
            tree.setTitle(obj.getName());
            if(level > 1){
                tree.setChildren(new ArrayList<>());
                Integer i2 = 0;
                IndustryGb obj2 = null;
                if(list2.size() > 0){
                    obj2 = list2.get(i2);
                }
                while(null != obj2){
                    if(null != obj2.getCode() && obj2.getCode().substring(0,4).equals(obj.getCode().substring(0,4))){
                        ReportTreeDTO data = new ReportTreeDTO();
                        BeanUtils.copyProperties(obj2,data);
                        data.setKey(obj2.getId());
                        data.setTitle(obj2.getName());
                        if(level > 2 && list3.size() > 0){
                            data.setChildren(new ArrayList<>());
                            Integer i3 = 0;
                            IndustryGb obj3 = null;
                            if(list3.size() > 0){
                                obj3 = list3.get(i3);
                            }
                            while(null != obj3){
                                if(null != obj3.getCode() && obj3.getCode().substring(0,6).equals(obj2.getCode().substring(0,6))){
                                    ReportTreeDTO data3 = new ReportTreeDTO();
                                    BeanUtils.copyProperties(obj3,data3);
                                    data3.setKey(obj3.getId());
                                    data3.setTitle(obj3.getName());
                                    if(level > 3 && list4.size() > 0){
                                        data3.setChildren(new ArrayList<>());
                                        Integer i4 = 0;
                                        IndustryGb obj4 = null;
                                        if(list4.size() > 0){
                                            obj4 = list4.get(i4);
                                        }
                                        while(null != obj4){
                                            if(null != obj4.getCode() && obj4.getCode().substring(0,8).equals(obj3.getCode().substring(0,8))){
                                                ReportTreeDTO data4 = new ReportTreeDTO();
                                                BeanUtils.copyProperties(obj4,data4);
                                                data4.setKey(obj4.getId());
                                                data4.setTitle(obj4.getName());
                                                data3.getChildren().add(data4);
                                                list4.remove(obj4);
                                                if(i4 >= list4.size()) break;
                                                obj4 = list4.get(i4);
                                            }
                                            else{
                                                ++i4;
                                                if(i4 >= list4.size()) break;
                                                obj4 = list4.get(i4);
                                            }
                                        }
                                    }
                                    
                                    data.getChildren().add(data3);
                                    list3.remove(obj3);
                                    if(i3 >= list3.size()) break;
                                    obj3 = list3.get(i3);
                                }
                                else{
                                    ++i3;
                                    if(i3 >= list3.size()) break;
                                    obj3 = list3.get(i3);
                                }
                            }
                        }
                        
                        //找到了就删除，提高循环效率
                        tree.getChildren().add(data);
                        list2.remove(obj2);
                        if(i2 >= list2.size()) break;
                        obj2 = list2.get(i2);
                    }
                    else{
                        ++i2;
                        if(i2 >= list2.size()) break;
                        obj2 = list2.get(i2);
                    }
                }
            }
            
            resp.add(tree);
        }
        
        ResponseInfo<List<ReportTreeDTO>> data = new ResponseInfo<>();
        data.setData(resp);
        return data;
    }
    
    /**
     * 经济圈列表
     */
    @ApiOperation(value = "经济圈列表", notes = "经济圈列表")
    @PostMapping("/list-economic_circle")
    public ResponseInfo<List<EconomicCircle>> listEconomicCircle(
            @ApiParam(value = "经济圈列表") @Valid @RequestBody EconomicCircle req) {
        log.info("BaseController.listEconomicCircle start...");
        
        List<EconomicCircle> list = economicCircleService.queryList(req);
        
        ResponseInfo<List<EconomicCircle>> data = new ResponseInfo<>();
        data.setData(list);
        return data;
    }
    
    /**
     * 公告类型列表
     */
    @ApiOperation(value = "公告类型列表", notes = "公告类型列表")
    @PostMapping("/list-notice_type")
    public ResponseInfo<List<NoticeType>> listNoticeType(
            @ApiParam(value = "公告类型列表") @Valid @RequestBody NoticeType req) {
        log.info("BaseController.listNoticeType start...");
        
        List<NoticeType> list = noticeTypeService.queryList(req);
        
        ResponseInfo<List<NoticeType>> data = new ResponseInfo<>();
        data.setData(list);
        return data;
    }
    
    /**
     * 研报类型列表
     */
    @ApiOperation(value = "研报类型列表", notes = "研报类型列表")
    @PostMapping("/list-report_type")
    public ResponseInfo<List<ReportType>> listReportType(
            @ApiParam(value = "研报类型列表") @Valid @RequestBody ReportType req) {
        log.info("BaseController.listReportType start...");
        
        List<ReportType> list = reportTypeService.queryList(req);
        
        ResponseInfo<List<ReportType>> data = new ResponseInfo<>();
        data.setData(list);
        return data;
    }
    
    /**
     * 研报类型树状列表
     */
    @ApiOperation(value = "研报类型树状列表", notes = "研报类型树状列表")
    @PostMapping("/list-report-type-tree")
    public ResponseInfo<List<ReportTreeDTO>> listReportTypeTree(
            @ApiParam(value = "研报类型树状列表") @Valid @RequestBody ReportTreeDTO req) {
        log.info("BaseController.listReportTypeTree start...");
    
        req.setLevel(1);
        List<ReportType> list = reportTypeService.queryLevel(req);
        
        req.setLevel(2);
        req.setIds(null);
        List<ReportType> list2 = reportTypeService.queryLevel(req);
        
        List<ReportTreeDTO> resp = new ArrayList<>();
    
        for(ReportType obj : list){
            ReportTreeDTO tree = new ReportTreeDTO();
            BeanUtils.copyProperties(obj,tree);
            tree.setKey(obj.getId());
            tree.setTitle(obj.getName());
            tree.setChildren(new ArrayList<>());
            
            for(ReportType obj2 : list2){
                 if(null != obj2.getParentId() && obj2.getParentId().equals(obj.getId())){
                     ReportTreeDTO data = new ReportTreeDTO();
                     BeanUtils.copyProperties(obj2,data);
                     data.setKey(obj2.getId());
                     data.setTitle(obj2.getName());
                     tree.getChildren().add(data);
                }
            }
    
            resp.add(tree);
        }
        
        ResponseInfo<List<ReportTreeDTO>> data = new ResponseInfo<>();
        data.setData(resp);
        return data;
    }
    
    /**
     * 风险类型关系
     */
    @ApiOperation(value = "风险类型关系", notes = "风险类型关系")
    @PostMapping("/list-risk-type")
    public ResponseInfo<List<RiskType>> listRiskType(
            @ApiParam(value = "风险类型关系") @Valid @RequestBody RiskType req) {
        log.info("BaseController.listRiskType start...");
        
        List<RiskType> list = riskTypeService.queryList(req);
        
        ResponseInfo<List<RiskType>> data = new ResponseInfo<>();
        data.setData(list);
        return data;
    }
    
    @ApiOperation(value = "用户部门树形", notes = "用户部门树形")
    @PostMapping("/user-dept-tree")
    public ResponseInfo<List<TreeDate>> treeDate(){
        //获取部门用户树
        List<TreeDate> list = treeDateService.treeDate();
        List<TreeDate> treeDates = null;
        if(list != null && list.size() > 0) {
            treeDates = TreeUtils.buildByRecursive(list);
        }
        return new ResponseInfo(treeDates);
    }
    
    /**
     * word转Pdf
     */
    @ApiOperation(value = "word转Pdf", notes = "word转Pdf")
    @PostMapping("/word-to-pdf")
    @PermitAll
    public ResponseInfo<PrivateReport> wordToPdf(@ApiParam(value = "word转Pdf") @Valid @RequestBody PrivateReport req) throws Exception {
        log.info("BaseController.wordToPdf start...");
        long start = System.currentTimeMillis();
        
        String ext = req.getFilePath().substring(req.getFilePath().lastIndexOf('.'));
        
        String directory = System.getProperty("java.io.tmpdir");
        String toFile = UUID.randomUUID().toString() + ".pdf";
    
        String fileName = UUID.randomUUID().toString() + ext;
        File file = new File(directory,fileName);
        fileService.download(req.getFilePath(),file);
        log.info("下载文件用时：" + (System.currentTimeMillis() - start) + "ms.");
    
        FileUtils.wordToPDF(directory + fileName,directory + toFile);
        log.info("转换文件用时：" + (System.currentTimeMillis() - start) + "ms.");
        //上传minio
        File tempFile = new File(directory,toFile);
        PDDocument doc = PDDocument.load(tempFile);
        //页数
        req.setPages(doc.getNumberOfPages());

        String path = minioUpload + DateUtils.getTodayString() + "/";
        String retPath = fileService.upload(tempFile, path, true);
        req.setPdfPath(retPath);
        try {
            if (!tempFile.delete()) {
                log.error("删除本地临时文件错误：{}", toFile);
            }
            if (!file.delete()) {
                log.error("删除本地临时文件错误：{}", fileName);
            }
        } catch (Exception e) {
            log.error("删除本地临时文件错误：{}", fileName);
        }
        log.info("上传文件用时：" + (System.currentTimeMillis() - start) + "ms.");
        
        ResponseInfo<PrivateReport> data = new ResponseInfo<>();
        data.setData(req);
        return data;
    }
    
    
    @ApiOperation("在线查看pdf")
    @DownloadMapping("/view-pdf")
    public void viewPdf(@ApiParam("pdf路径参数") FileReqVO vo, HttpServletResponse response) throws IOException {
        response.setContentType("application/pdf");
    
        String getPath = null;
        if(null != vo.getReportId()){
            PrivateReport pr = privateReportService.queryById(vo.getReportId());
            getPath = pr.getPdfPath();
        }
        else if(null != vo.getMaterialId()){
            PrivateMaterial pr = privateMaterialService.queryById(vo.getMaterialId());
            getPath = pr.getPdfPath();
        }
        else if(null != vo.getPath()){
            getPath = vo.getPath();
        }
        else{
            throw new IirException(IirErrorType.INTFACE_PARAMS_ERROR,"ReportId");
        }
        //String getPath = vo.getPath();
        if(getPath.indexOf(minioAddress) >= 0){
            getPath = getPath.substring(minioAddress.length());
        }
        String ext = getPath.substring(getPath.lastIndexOf('.'));
        String directory = System.getProperty("java.io.tmpdir");
        String fileName = UUID.randomUUID().toString() + ext;
        File file = new File(directory,fileName);
        fileService.download(getPath,file);

        if (file.exists()){
            byte[] data = null;
            try {
                FileInputStream input = null;
                if(null != vo.getWaterMark() && vo.getWaterMark().equals(1)){
                    User loginUser = (User) SecurityUtils.getLoginUser().getAdditionalInfo();
                    String waterMark = loginUser.getName() + " " + DateUtils.toString(new Date(),"yyyyMMdd");
                    String fileName2 = UUID.randomUUID().toString() + ext;
                    FileUtils.waterMarkPdf(directory + File.separator + fileName,directory + File.separator + fileName2,waterMark);
                    File file2 = new File(directory + File.separator + fileName2);
                    input = new FileInputStream(file2);
                }
                else{
                    input = new FileInputStream(file);
                }
                data = new byte[input.available()];
                input.read(data);
                response.getOutputStream().write(data);
                input.close();
            } catch (Exception e) {
                log.error("viewPdf错误：" ,e);
            }

        }

    }
    
    @ApiOperation("下载文件")
    @DownloadMapping("/file-down")
    public void fileDown(@ApiParam("请求参数") FileReqVO vo, HttpServletResponse response) throws IOException {
        String getPath = vo.getPath();
        if(getPath.indexOf(minioAddress) >= 0){
            getPath = getPath.substring(minioAddress.length());
        }
        String ext = getPath.substring(getPath.lastIndexOf('.'));
        String directory = System.getProperty("java.io.tmpdir");
        String fileName = UUID.randomUUID().toString() + ext;
        File file = new File(directory,fileName);
        fileService.download(getPath,file);
        if(vo.getLog().equals(Constant.WHETHER_YES)){
            if(null != vo.getMaterialId()) {
                //在内部研报操作日志表新建一条记录
                MaterialLog reportLog = new MaterialLog();
                reportLog.setId(myIdGenerator.nextId());
                //根据研报ID获取研报名称
                String reportTitle = privateMaterialService.queryReportTitleById(vo.getMaterialId());
                reportLog.setReportId(vo.getMaterialId());
                reportLog.setTitle(reportTitle);
                reportLog.setOptType("下载文件");
                reportLog.setCreateTime(new Date());
                User loginUser = (User) SecurityUtils.getLoginUser().getAdditionalInfo();
                reportLog.setCreateUserId(loginUser.getId());
                materialLogService.add(reportLog);
            }
            else {
                //在内部研报操作日志表新建一条记录
                ReportLog reportLog = new ReportLog();
                reportLog.setId(myIdGenerator.nextId());
                //根据研报ID获取研报名称
                String reportTitle = privateReportService.queryReportTitleById(vo.getReportId());
                reportLog.setReportId(vo.getReportId());
                reportLog.setTitle(reportTitle);
                reportLog.setOptType("下载文件");
                reportLog.setCreateTime(new Date());
                User loginUser = (User) SecurityUtils.getLoginUser().getAdditionalInfo();
                reportLog.setCreateUserId(loginUser.getId());
                reportLogService.add(reportLog);
            }
        }
        
        if (file.exists()){
            try {
                localFileService.download(file.getPath(), response);
            } catch (Exception e) {
                log.error("fileDown错误：" ,e);
            }
            
        }
    }
    
    @ApiOperation("下载文件加水印")
    @DownloadMapping("/file-down-water")
    public void fileDownWater(@ApiParam("请求参数") FileReqVO vo, HttpServletResponse response) throws IOException {
        String getPath = vo.getPath();
        if(getPath.indexOf(minioAddress) >= 0){
            getPath = getPath.substring(minioAddress.length());
        }
        String ext = getPath.substring(getPath.lastIndexOf('.'));
        String directory = System.getProperty("java.io.tmpdir");
        String fileName = UUID.randomUUID().toString() + ext;
        File file = new File(directory,fileName);
        fileService.download(getPath,file);
    
        if(vo.getLog().equals(Constant.WHETHER_YES)){
            if(null != vo.getMaterialId()) {
                //在内部研报操作日志表新建一条记录
                MaterialLog reportLog = new MaterialLog();
                reportLog.setId(myIdGenerator.nextId());
                //根据研报ID获取研报名称
                String reportTitle = privateMaterialService.queryReportTitleById(vo.getMaterialId());
                reportLog.setReportId(vo.getMaterialId());
                reportLog.setTitle(reportTitle);
                reportLog.setOptType("下载文件");
                reportLog.setCreateTime(new Date());
                User loginUser = (User) SecurityUtils.getLoginUser().getAdditionalInfo();
                reportLog.setCreateUserId(loginUser.getId());
                materialLogService.add(reportLog);
            }
            else {
                //在内部研报操作日志表新建一条记录
                ReportLog reportLog = new ReportLog();
                reportLog.setId(myIdGenerator.nextId());
                //根据研报ID获取研报名称
                String reportTitle = privateReportService.queryReportTitleById(vo.getReportId());
                reportLog.setReportId(vo.getReportId());
                reportLog.setTitle(reportTitle);
                reportLog.setOptType("下载文件");
                reportLog.setCreateTime(new Date());
                User loginUser = (User) SecurityUtils.getLoginUser().getAdditionalInfo();
                reportLog.setCreateUserId(loginUser.getId());
                reportLogService.add(reportLog);
            }
        }
        
        if (file.exists()){
            try {
                User loginUser = (User) SecurityUtils.getLoginUser().getAdditionalInfo();
                String waterMark = loginUser.getName() + " " + DateUtils.toString(new Date(),"yyyyMMdd");
                String fileName2 = UUID.randomUUID().toString() + ext;
                if(vo.getType().equals(1)){//pdf
                    FileUtils.waterMarkPdf(directory + File.separator + fileName,directory + File.separator + fileName2,waterMark);
                }
                else if(vo.getType().equals(2)) {//excel
                    FileUtils.waterMarkExcel(directory + File.separator + fileName,directory + File.separator + fileName2,waterMark);
                }
                else{
                    throw new IirException(IirErrorType.INTFACE_PARAMS_ERROR,"type");
                }
                File file2 = new File(directory + File.separator + fileName2);
                localFileService.download(file2.getPath(), response);
                
            } catch (Exception e) {
                log.error("fileDown错误：" ,e);
            }
            
        }
    }

}
